/**
 * nblog函数库
 *
 +-------------------------------------------------
 * @link
 * @copyright
 * @author Haijun Wu <nicholasinlove@126.com>
 +------------------------------------------------- 
 * 
 * $Id: common.js 93 2011-04-06 10:17:48Z nicholasinlove1986@gmail.com $
 */
 
 
(function() {
	
	function reply(authorId, commentId, commentBox) {
		var author = document.getElementById(authorId).innerHTML;
		var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + ' </a> \n';
		appendReply(insertStr, commentBox);
	}
	
	function quote(authorId, commentId, commentBodyId, commentBox) {
		var author = document.getElementById(authorId).innerHTML;
		var comment = document.getElementById(commentBodyId).innerHTML;
		var insertStr = '<blockquote cite="#' + commentBodyId + '">';
		insertStr += '\n<strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :</strong>';
		insertStr += comment.replace(/\t/g, "");
		insertStr += '</blockquote>\n';
		insertQuote(insertStr, commentBox);
	}
	
	function appendReply(insertStr, commentBox) {
		if(document.getElementById(commentBox) && document.getElementById(commentBox).type == 'textarea') {
			field = document.getElementById(commentBox);
		} else {
			alert("The comment box does not exist!");
			return false;
		}
		if (field.value.indexOf(insertStr) > -1) {
			alert("You've already appended this reply!");
			return false;
		}
		if (field.value.replace(/\s|\t|\n/g, "") == '') {
			field.value = insertStr;
		} else {
			field.value = field.value.replace(/[\n]*$/g, "") + '\n\n' + insertStr;
		}
		field.focus();
	}
	
	function insertQuote(insertStr, commentBox) {
		if(document.getElementById(commentBox) && document.getElementById(commentBox).type == 'textarea') {
			field = document.getElementById(commentBox);
		} else {
			alert("The comment box does not exist!");
			return false;
		}
		if(document.selection) {
			field.focus();
			sel = document.selection.createRange();
			sel.text = insertStr;
			field.focus();
		} else if (field.selectionStart || field.selectionStart == '0') {
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			var cursorPos = startPos;
			field.value = field.value.substring(0, startPos)
						  + insertStr
						  + field.value.substring(endPos, field.value.length);
			cursorPos += insertStr.length;
			field.focus();
			field.selectionStart = cursorPos;
			field.selectionEnd = cursorPos;
		} else {
			field.value += insertStr;
			field.focus();
		}
	}
	
	function loadCommentShortcut(frm, submitbnt, desc) {
		document.getElementById(frm).onkeydown = function (moz_ev) {
			var ev = null;
			ev = window.event ? window.event : moz_ev;
			if (ev != null && ev.ctrlKey && ev.keyCode == 13) {
				document.getElementById(submitbnt).click();
			}
		};
		document.getElementById(submitbnt).value += desc;
	}
	
	function overComment(commentNode) {
		var actionNode = commentNode.find('div.count');
		if (actionNode.length > 0) {
			actionNode.fadeIn(200);
			return
		}
		var anchor = commentNode.find('a.anchor');
		var commentId = anchor.attr('href').slice(9);
		var wrap = document.createElement('div');
		wrap.className = 'count';
		var replyButton = document.createElement('a');
		replyButton.href = '#respond';
		replyButton.innerHTML = 'Reply';
		jQuery(replyButton).click(function() {
			reply('reviewer-' + commentId, 'comment-' + commentId, 'comment')
		});
		wrap.appendChild(replyButton);
		var sepBetweenReplyAndQuote = document.createTextNode(' | ');
		wrap.appendChild(sepBetweenReplyAndQuote);
		var quoteButton = document.createElement('a');
		quoteButton.href = '#respond';
		quoteButton.innerHTML = 'Quote';
		jQuery(quoteButton).click(function() {
			quote('reviewer-' + commentId, 'comment-' + commentId, 'commentbody-' + commentId, 'comment')
		});
		wrap.appendChild(quoteButton);
		if (/gd/.test(anchor.attr('rel'))) {
			var sepBetweenQuoteAndEdit = document.createTextNode(' | ');
			wrap.appendChild(sepBetweenQuoteAndEdit);
			var editButton = document.createElement('a');
			editButton.href = ROOT + '/admincp.php/Guestbook/detailsMessage/id/' + commentId;
			editButton.innerHTML = 'Edit';
			wrap.appendChild(editButton)
		}
		else if (/co/.test(anchor.attr('rel'))) {
			var sepBetweenQuoteAndEdit = document.createTextNode(' | ');
			wrap.appendChild(sepBetweenQuoteAndEdit);
			var editButton = document.createElement('a');
			editButton.href = ROOT + '/admincp.php/Comment/detailsComment/id/' + commentId;
			editButton.innerHTML = 'Edit';
			wrap.appendChild(editButton)
		}
		jQuery(wrap).hide().prependTo(commentNode.find('div.userinfo')).fadeIn(200)
	}
	
	function outComment(commentNode) {
		commentNode.find('div.count').fadeOut(400)
	}
	
	function fleshVerify() {
		var timenow = new Date().getTime();
		$('#verifyImg').attr('src', URL+'/verify/'+timenow);
	}
	
	function delayComment(delayTime, delayText, submitText, submitButton) {
		if (--delayTime > 0) {
			setTimeout(function() {
				submitButton.attr('disabled', 'disabled');
				submitButton.attr('value', delayTime + delayText);
				delayComment(delayTime, delayText, submitText, submitButton);
			},
			1000)
		} else {
			submitButton.attr('value', submitText);
			submitButton.removeAttr('disabled')
		}
	}
	
	function complete(data,status){
		if (status==1) {	
			//window.location.href = URT+'#comment-'+data;
			//window.location.reload();
			document.getElementById('comment').value = '';
			var commentList = jQuery('#thecomments');
			var submitText = $('#cmt-submit').attr('value');
			var delayTime = 10;
			var delayText = ' sec wait before next submit';
			delayComment(delayTime, delayText, submitText, $('#cmt-submit'));
			if (commentList.length <= 0) {
				jQuery('#commentnavi').before('<ol id="thecomments" class="commentlist"></ol>')
			} else if (commentList.length == 1) {
				var commentNode = commentList.find('li:last');
				if (!/comment/.test(commentNode.attr('class'))) {
					commentNode.hide()
				}
			}
			commentList.append(data);
			commentList.find('li:last').hide().slideDown(400).hover(function() {
				overComment(jQuery(this))
			},
			function() {
				outComment(jQuery(this))
			}).find('a[rel*="external"]').click(function() {
				window.open(this.href);
				return false
			})
		}
	}

	jQuery.fn.cumulativeOffset = function(e) {
        var offset = {left: 0,top: 0};
        do {
            offset.left += e.offsetLeft || 0;
            offset.top += e.offsetTop || 0;
            e = e.offsetParent
        }
        while (e);
        return offset
    };

	$(document).ready(function(){
	
		//init_menu(movemenu_location);
		
		var content = $("#think_run_time").html();
		$("#runtime").html(content);
		$("#think_run_time").remove();
		
		$('#verify').blur(function(){
			hiddenVerify();
		}).focus(function(){
			focusGetVerify(this);            
		});
		
		$("a.thickbox").fancybox({
			'opacity'		: true,
			'overlayShow'	: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});
		
		$("a#example1").fancybox();

		$("a#example2").fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack'
		});


		$("a#example3").fancybox({
			'transitionIn'	: 'none',
			'transitionOut'	: 'none'	
		});

		$("a#example4").fancybox({
			'opacity'		: true,
			'overlayShow'	: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'none'
		});

		$("a#example5").fancybox();

		$("a#example6").fancybox({
			'titlePosition'		: 'outside',
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.9
		});

		$("a#example7").fancybox({
			'titlePosition'	: 'inside'
		});

		$("a#example8").fancybox({
			'titlePosition'	: 'over'
		});

		$("a[rel=example_group]").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
		
		$('.widget_recent_comments li.rc_item').hover(function() {
			jQuery('.rc_expand',this).fadeIn(200);
		},
		function() {
			jQuery('.rc_expand',this).fadeOut(400);
		});
		
		$('a.highslide-image').click(function() {
            window.open(this.href);
            return false
        });
		
        $('a[rel*="external"]').click(function() {
            window.open(this.href);
            return false
        });
		
		if (document.getElementById('comments')) {
		
			$('#show_author_info').toggle(function() {
				jQuery('#author_info').slideDown();
				jQuery(this).html('Close &laquo;')
			},
			function() {
				jQuery('#author_info').slideUp();
				jQuery(this).html('Change &raquo;');
				jQuery('#welcome-row strong').html(jQuery('#author').val())
			});
			
			/*$('#thecomments li.comment').hover(function() {
				jQuery('.count',this).fadeIn(200);
			},
			function() {
				jQuery('.count',this).fadeOut(400);
			});*/
			
			jQuery('#thecomments li.comment').hover(function() {
				overComment(jQuery(this))
			},
			function() {
				outComment(jQuery(this))
			});
			
			loadCommentShortcut('commentform', 'cmt-submit', ' (Ctrl+Enter)');
			
			if (!jQuery.browser.opera) {
				var id = /^#comment-/;
				var at = /^@/;
				var commentList = jQuery('#thecomments');
				commentList.find('li p a').each(function() {
					if (jQuery(this).attr('href').match(id) && jQuery(this).text().match(at)) {
						jQuery(this).addClass('atreply')
					}
				});
				jQuery('.atreply').hover(function() {
					var target = this;
					var commentId = jQuery(this).attr('href');
					if (jQuery(commentId).is('.comment')) {
						jQuery('<li class="hreview tip"></li>').hide().html('<div class="arrow"></div><div class="innerbox">' + jQuery(commentId+'-').html() + '</div>').appendTo(jQuery('#thecomments'));
						commentList.find('.tip').css({
							left: jQuery().cumulativeOffset(this).left + jQuery(this).width() + 10,
							top: jQuery().cumulativeOffset(this).top - 22
						}).show({
							opacity: '1'
						})
					} else {
						var id = commentId.slice(9);
						jQuery.ajax({
							type: 'GET',
							url: APP+'/Comment/detail/act/detail_ajax/id/' + id,
							dataType: 'html',
							contentType: 'charset=UTF-8',
							beforeSend: function() {
								jQuery('<li class="hreview tip"></li>').hide().html('<div class="arrow"></div><div class="innerbox"><p class="ajax-loader msg">Loading...</p></div>').appendTo(jQuery('#thecomments'));
								commentList.find('.tip').css({
									left: jQuery().cumulativeOffset(target).left + jQuery(target).width() + 10,
									top: jQuery().cumulativeOffset(target).top - 22
								}).show({
									opacity: '1'
								})
							},
							success: function(data) {
								var addedComment = jQuery(data);
								addedComment.hide().appendTo(commentList);
								commentList.find('.tip .innerbox').html(jQuery(data))
							},
							error: function() {
								commentList.find('.tip .innerbox').html('<p class="msg">Oops, failed to load data.</p>')
							}
						})
					}
				},
				function() {
					commentList.find('.tip').fadeOut(200, 
					function() {
						jQuery(this).remove()
					})
				})
			}
		}

		jQuery('#cmt-submit').click(function() {
			ThinkAjax.sendForm('commentform',APP+'/Comment/post',complete,'commentform-message');
		});
		
		searchboxInit('searchtxt', 'Type text to search here...');
		
	});
	
	function searchboxInit(textfileId, tip) {
		var searchtxt = document.getElementById(textfileId);
		if(searchtxt.value == "" || searchtxt.value == tip) {
			searchtxt.className += " searchtip";
			searchtxt.value = tip;
		}
		searchtxt.onfocus = function(e) {
			if(searchtxt.value == tip) {
				searchtxt.value = "";
				searchtxt.className = searchtxt.className.replace(" searchtip", "");
			}
		}
		searchtxt.onblur = function(e) {
			if(searchtxt.value == "") {
				searchtxt.className += " searchtip";
				searchtxt.value = tip;
			}
		}
	}

})();

(function(h){h.browser=h.extend({chrome:(/chrome/i).test(navigator.userAgent)},h.browser);var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&(h.browser.safari||h.browser.chrome)?g.body:i?(g.compatMode&&g.compatMode!=="BackCompat"?g.documentElement:g.body):this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

/* 无间隙自动换行滚动的Discuz论坛公告 */
function ss(id) {
	return document.getElementById(id);
}

function announcement() {
	var ann = new Object();
	ann.anndelay = 3000;
	ann.annst = 0;
	ann.annstop = 0;
	ann.annrowcount = 0;
	ann.anncount = 0;
	ann.annlis = ss('annbody').getElementsByTagName("LI");
	ann.annrows = new Array();
	ann.announcementScroll = function () {
		if(this.annstop) { this.annst = setTimeout(function () { ann.announcementScroll(); }, this.anndelay);return; }
		if(!this.annst) {
			var lasttop = -1;
			for(i = 0;i < this.annlis.length;i++) {
				if(lasttop != this.annlis[i].offsetTop) {
					if(lasttop == -1) lasttop = 0;
					this.annrows[this.annrowcount] = this.annlis[i].offsetTop - lasttop;this.annrowcount++;
				}
				lasttop = this.annlis[i].offsetTop;
			}
			if(this.annrows.length == 1) {
				ss('ann').onmouseover = ss('ann').onmouseout = null;
			} else {
				this.annrows[this.annrowcount] = this.annrows[1];
				ss('annbodylis').innerHTML += ss('annbodylis').innerHTML;
				this.annst = setTimeout(function () { ann.announcementScroll(); }, this.anndelay);
				ss('ann').onmouseover = function () { ann.annstop = 1; };
				ss('ann').onmouseout = function () { ann.annstop = 0; };
			}
			this.annrowcount = 1;
			return;
		}
		if(this.annrowcount >= this.annrows.length) {
			ss('annbody').scrollTop = 0;
			this.annrowcount = 1;
			this.annst = setTimeout(function () { ann.announcementScroll(); }, this.anndelay);
		} else {
			this.anncount = 0;
			this.announcementScrollnext(this.annrows[this.annrowcount]);
		}
	};
	ann.announcementScrollnext = function (time) {
		ss('annbody').scrollTop++;
		this.anncount++;
		if(this.anncount != time) {
			this.annst = setTimeout(function () { ann.announcementScrollnext(time); }, 10);
		} else {
			this.annrowcount++;
			this.annst = setTimeout(function () { ann.announcementScroll(); }, this.anndelay);
		}
	};
	ann.announcementScroll();
}
